#!/bin/sh

. /etc/utils.sh

INTERFACES=
APIMODE=

if is-fiberjack; then
  INTERFACES="pon0"
elif is-spacecast; then
  INTERFACES="lan0"
elif is-network-box; then
  INTERFACES="wan0"
  APIMODE="--api-mode=iff"
  # Initialize to the down state.
  /etc/ifplugd/ifplugd.action wan0 down
elif is-wireless-client; then
  INTERFACES="eth0 wcli0 wcli1"
  APIMODE="--api-mode=iff"
  mkdir -p /tmp/conman/interfaces
fi

start()
{
  for IFNAME in $INTERFACES; do
    if interface_exists $IFNAME && ! is_quantenna_interface $IFNAME; then
      babysit 60 ifplugd $APIMODE -i $IFNAME -n -s -f -I -q -d0 2>&1 | logos "ifplugd-$IFNAME" &
    fi
  done
}

stop()
{
  for IFNAME in $INTERFACES; do
    ifplugd -k -W -i $IFNAME
  done
  pkill ifplugd.action
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  sleep)
    sleep 60
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    ;;
esac

exit 0
